//
//  ScenicSpotViewController.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/4/12.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "ScenicSpotViewController.h"
#import "ScenicSpotCell.h"
#import "ScenicDescribeVC.h"

#define pagesize  10
@interface ScenicSpotViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView * collectionView;
@property (nonatomic, strong)NSMutableArray * dataSource;

@property (nonatomic)NSInteger  pageno;
@end

@implementation ScenicSpotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setBg];
    [self setUp];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"5"];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [MobClick endLogPageView:@"5"];
}

- (void)dealloc {
    
    [MobClick event:@"5_1_1"];
}

- (void)setUp {
    
    self.barBGAlpha = 0;
    
    [self createCollectionView];
    
    @weakify(self);
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        self.pageno = 0;
        [self.collectionView.mj_footer resetNoMoreData];
        [self loadDataSource];
    }];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        [self loadDataSource];
    }];
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self loadDataSource];
}

- (void)loadDataSource {
    
    @weakify(self);
    [NetAPIManager  getAllViewsWithCity:self.city.cid start:[NSNumber numberWithInteger:_pageno] count:@(pagesize) block:^(NSArray *viewsArray, NSError *error) {
        @strongify(self);
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        // [self.imageView setImageURL:[NSURL URLWithString:city.pictures] placeholderImage:[UIImage imageNamed:@"city.jpeg"]];
        
        if (viewsArray.count < 10) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        } else {
            
            [self.collectionView.mj_footer endRefreshing];
        }
        if (self.pageno == 0) {
            self.dataSource =  [NSMutableArray arrayWithArray:viewsArray];
            [self.collectionView.mj_header endRefreshing];
            
            if (viewsArray.count < 10) {
                self.collectionView.mj_footer = nil;
            } else {
                self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                    @strongify(self);
                    [self loadDataSource];
                }];
            }
        }else {
            [self.dataSource addObjectsFromArray:viewsArray];
             [self.collectionView.mj_footer endRefreshing];
        }
        
        if (viewsArray.count < pagesize) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        }
        self.pageno ++;
        [self.collectionView reloadData];

    }];
}

- (void)setBg {
    
    self.view.backgroundColor = kThemeColor;
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = self.Bgimage;
    imageView.backgroundColor = kThemeColor;
    [self.view addSubview:imageView];
    
}

- (void)createCollectionView {
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //layout.minimumInteritemSpacing = 20;//
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,kNavigationBarHeight, self.view.frame.size.width, SCREEN_HEIGHT - kNavigationBarHeight) collectionViewLayout:layout];
    
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"ScenicSpotCell" bundle:nil] forCellWithReuseIdentifier:@"ScenicSpotCell"];
    
    // ios 11  scrollvew 的位置调整
    if (@available(iOS 11.0, *)) {
        self.collectionView.contentInsetAdjustmentBehavior =UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
//    [_collectionView registerClass:[CustomCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];             //注册头视图
    //    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];              //注册尾视图
   
    
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ScenicSpotCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ScenicSpotCell" forIndexPath:indexPath];
    
    Scenic *scenic = self.dataSource[indexPath.row];
    
    [cell configScenicWith:scenic];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((SCREEN_WIDTH -30)/2, (SCREEN_WIDTH -30)/2 + 115);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ScenicDescribeVC *scenicDesVC = [ScenicDescribeVC new];
    Scenic *scenic = self.dataSource[indexPath.row];
    scenicDesVC.scenic = scenic;
    [self.navigationController pushViewController:scenicDesVC animated:YES];
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    [MobClick event:@"5_1_2"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
